package com.luculent.util.extension.api;


import com.luculent.util.extension.enums.ErrorCodeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * 非分页数据通用返回格式
 *
 * @param <T>
 * @author: zhangyang
 * @since: 2019年7月5日上午11:03:00
 */
@ApiModel
public class Result<T> extends DataResult<T> {

	/**
	 * 响应中的对象.
	 */
	@ApiModelProperty(value = "响应中的对象")
	protected T data;

	public Result() {
		this.code = IErrorCode.RESULT_OK_CODE;
		this.msg = IErrorCode.DEFAULT_OK_MESSAGE;
	}

	public Result(T data) {
		this.code = IErrorCode.RESULT_OK_CODE;
		this.msg = IErrorCode.DEFAULT_OK_MESSAGE;
		this.data = data;
	}

	public Result(IErrorCode errorCode, String msg) {
		this.code = errorCode.getCode();
		this.msg = msg;
	}

	public Result(IErrorCode errorCode, String msg, T data) {
		this.code = errorCode.getCode();
		this.msg = msg;
		this.data = data;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	public static Result<Void> ok_void() {
		return new Result<Void>(ErrorCodeEnum.OK, IErrorCode.DEFAULT_OK_MESSAGE);
	}

	public static <T> Result<T> ok() {
		return new Result<T>(ErrorCodeEnum.OK, IErrorCode.DEFAULT_OK_MESSAGE);
	}

	public static <T> Result<T> ok_msg(String msg) {
		return new Result<T>(ErrorCodeEnum.OK, msg);
	}

	public static <T> Result<T> ok(T data) {
		return new Result<T>(ErrorCodeEnum.OK, IErrorCode.DEFAULT_OK_MESSAGE, data);
	}


	public static <T> Result<T> fail() {
		return new Result<T>(ErrorCodeEnum.FAIL, ErrorCodeEnum.FAIL.getMsg());
	}

	public static <T> Result<T> fail(IErrorCode errorCode, String msg) {
		return new Result<T>(errorCode, msg);
	}

	public static <T> Result<T> fail_msg(String msg) {
		return new Result<T>(ErrorCodeEnum.FAIL, msg);
	}

	public static <T> Result<T> fail(IErrorCode errorCode) {
		return new Result<T>(errorCode, errorCode.getMsg());
	}

	public static <T> Result<T> fail(T data, IErrorCode errorCode) {
		return new Result<T>(errorCode,errorCode.getMsg(), data);
	}

}
